package net.csdn.business.discuss.api.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import net.csdn.business.discuss.api.mapper.DiscussAnswerRecordMapper;
import net.csdn.business.discuss.api.model.dto.NewDiscussAnswerRecordDTO;
import net.csdn.business.discuss.api.model.entity.DiscussAnswerRecord;
import net.csdn.business.discuss.api.model.entity.DiscussVoteRecord;
import net.csdn.business.discuss.api.model.vo.AnswerSortVO;
import net.csdn.business.discuss.api.service.IDiscussAnswerRecordService;
import net.csdn.business.discuss.common.constant.RedisConstants;
import net.csdn.business.discuss.common.model.vo.UserInfoVO;
import net.csdn.business.discuss.feign.api.UserFeignClient;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 讨论采纳问题记录Service业务层处理
 * 
 * @author zhangyl
 * @date 2023-07-25
 */
@Slf4j
@Service
public class DiscussAnswerRecordServiceImpl extends ServiceImpl<DiscussAnswerRecordMapper, DiscussAnswerRecord> implements IDiscussAnswerRecordService
{


    @Autowired
    private UserFeignClient userFeignClient;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer insertDiscussAnswerRecord(NewDiscussAnswerRecordDTO params) {
        DiscussAnswerRecord discussAnswerRecord=new DiscussAnswerRecord();
        BeanUtils.copyProperties(params,discussAnswerRecord);
        String id= IdUtil.fastSimpleUUID();
        discussAnswerRecord.setObjectId(id);
        discussAnswerRecord.setCreatedBy(params.getCurrUserId());
        return baseMapper.insert(discussAnswerRecord);
    }

    @Override
    public Integer deleteAnswerRecordByDiscussId(String discussId) {
        return baseMapper.deleteAnswerRecordByDiscussId(discussId);
    }

    @Override
    @Cacheable(value = RedisConstants.DISCUSS_ANSWER_SORT_30_LIST, key = "#sourceId+'-'+#sourceType")
    public List<AnswerSortVO> getAnswerSort(String sourceId,Integer sourceType) {
        List<AnswerSortVO> result=baseMapper.selectAnswerSortByDiscussId(sourceId,sourceType);
        //批量查询用户信息
        if(result==null||result.size()==0){
            return result;
        }
        List<String> userIds=new ArrayList<>();
        result.stream().forEach(answerSortVO->{
            userIds.add(answerSortVO.getUserId());
        });
        try{
            Map<String, UserInfoVO> userInfoMap=userFeignClient.batchQuery(userIds);
            result.stream().forEach(answerSortVO->{
                String userId=answerSortVO.getUserId();
                UserInfoVO userInfoVO=userInfoMap.get(userId);
                if(userInfoVO!=null){
                    answerSortVO.setUserName(userInfoVO.getUsername());
                    answerSortVO.setUserPhoto(userInfoVO.getPhoto());
                }
            });
        }catch (Exception e){
            log.info("query remote user info error !");
        }
        return result;
    }
}
